package com.meixiang.adapter.myFundAdapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.Layout;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.meixiang.R;
import com.meixiang.entity.fund.FundAccountBean;
import com.meixiang.entity.fund.FundIndexResult;
import com.meixiang.util.AbStrUtil;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 我的基金--持有基金
 */
public class FundHoldAdapter extends RecyclerView.Adapter<FundHoldAdapter.MyViewHolder> {

    private Context mContext;
    private static final int TYPE_HEADER = 0 ;
    private static final int TYPE_NORMAL = 1 ;
    private View mHeaderView ; //頭部
    private int mHeaderCount = 1 ; //头部视图的个数
    private List<FundAccountBean.FundHoldingList> fund_holding_list ;

    public FundHoldAdapter(Context context) {
        this.mContext = context;
    }

    /**
     * 刷新数据
     */
    public void refreshData(List<FundAccountBean.FundHoldingList> list) {
        fund_holding_list = list;
        notifyDataSetChanged();
    }

    public void setHeaderView(View headerView) {
        mHeaderView = headerView;
        notifyItemInserted(0);
    }

    /**
     * 得到当前headerView
     * @return
     */
    public View getHeaderView() {
        return mHeaderView;
    }

    @Override
    public int getItemViewType(int position) {
        if(mHeaderView == null) return TYPE_NORMAL;
        if(position == 0) return TYPE_HEADER;
        return TYPE_NORMAL;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        if(mHeaderView != null && viewType == TYPE_HEADER) {
            return new MyViewHolder(mHeaderView);
        }
        View layout = LayoutInflater.from(mContext).inflate(R.layout.list_fund_hold_item, parent, false);
        return new MyViewHolder(layout);
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, int position) {
        if(getItemViewType(position) == TYPE_HEADER) return;
        final int pos = getRealPosition(holder);
        holder.fund_hold_item_title.setText(fund_holding_list.get(pos).getFund_name());
        holder.tv_fund_hole_item_money_name.setText(fund_holding_list.get(pos).getAmount_name());
        holder.tv_fund_hole_item_yesterday_name.setText(fund_holding_list.get(pos).getDay_rate_name());
        holder.tv_fund_hole_item_flow_profit_name.setText(fund_holding_list.get(pos).getFlow_profit_name());
        holder.tv_fund_item_money.setText(AbStrUtil.ChangeF3D(fund_holding_list.get(pos).getMoney()));
            if(fund_holding_list.get(pos).getDay_rate().contains("-")){
                if(fund_holding_list.get(pos).getProfit_yesterday().contains("-")){
                    holder.tv_fund_item_profit_yesterday.setText(AbStrUtil.ChangeF3D(fund_holding_list.get(pos).getProfit_yesterday())+"("+fund_holding_list.get(pos).getDay_rate()+"% )");
                }else{
                    holder.tv_fund_item_profit_yesterday.setText("+"+AbStrUtil.ChangeF3D(fund_holding_list.get(pos).getProfit_yesterday())+"("+fund_holding_list.get(pos).getDay_rate()+"% )");
                }
            }else{
                if(fund_holding_list.get(pos).getProfit_yesterday().contains("-")) {
                    holder.tv_fund_item_profit_yesterday.setText(AbStrUtil.ChangeF3D(fund_holding_list.get(pos).getProfit_yesterday()) + "(+" + fund_holding_list.get(pos).getDay_rate() + "%)");
                }else{
                    holder.tv_fund_item_profit_yesterday.setText("+" + AbStrUtil.ChangeF3D(fund_holding_list.get(pos).getProfit_yesterday()) + "(+" + fund_holding_list.get(pos).getDay_rate() + "%)");
                }
            }
        if(fund_holding_list.get(pos).getFlow_profit().contains("-")){
            holder.tv_fund_item_flow_profit.setText(AbStrUtil.ChangeF3D(fund_holding_list.get(pos).getFlow_profit()));
        }else{
            holder.tv_fund_item_flow_profit.setText("+"+AbStrUtil.ChangeF3D(fund_holding_list.get(pos).getFlow_profit()));
        }
        if (onItemClickListener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onItemClickListener.onItemClick(holder.itemView, pos);

                }
            });
        }
    }

    /**
     * 得到真實的position
     * @param holder
     * @return
     */
    public int getRealPosition(RecyclerView.ViewHolder holder) {
        int position = holder.getLayoutPosition();
        return mHeaderView == null ? position : position - 1;
    }

    private OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    @Override
    public int getItemCount() {
        return fund_holding_list == null?mHeaderCount:fund_holding_list.size()+mHeaderCount;
    }

   class MyViewHolder extends RecyclerView.ViewHolder {

       @Bind(R.id.fund_hold_item_title)
       TextView fund_hold_item_title ;
       @Bind(R.id.tv_fund_hole_item_money_name)
       TextView tv_fund_hole_item_money_name ;
       @Bind(R.id.tv_fund_hole_item_yesterday_name)
       TextView tv_fund_hole_item_yesterday_name ;
       @Bind(R.id.tv_fund_hole_item_flow_profit_name)
       TextView tv_fund_hole_item_flow_profit_name ;
       @Bind(R.id.tv_fund_item_money)
       TextView tv_fund_item_money ;
       @Bind(R.id.tv_fund_item_profit_yesterday)
       TextView tv_fund_item_profit_yesterday ;
       @Bind(R.id.tv_fund_item_flow_profit)
       TextView tv_fund_item_flow_profit ;
        public MyViewHolder(View itemView) {
            super(itemView);
            if(itemView == mHeaderView) return;
            ButterKnife.bind(this, itemView);
        }
    }
}